import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/components/layout/index'
import NotFound from '@/components/layout/NotFound'
import Dashboard from '@/views/dashboard/index'
import Account from '@/views/account/index'
import UserList from '@/views/user/index'
import groupList from '@/views/group/index'
import orderList from '@/views/order/index'
import orderDetails from '@/views/order/details'
import orderCreate from '@/views/order/create'

import Login from '@/views/account/login'
import Auth from '@/views/account/auth'

const routes = [
  // {
  //   path: '/',
  //   redirect: '/dashboard',
  //   component: Layout,
  //   meta: { title: '首页' },
  //   children: [{
  //     path: '/dashboard',
  //     component: Dashboard,
  //     name: 'dashboard',
  //     meta: {
  //       featureId: 'dashboard',
  //       featureName: '我的看板'
  //     }
  //   },
  //   {
  //     path: '/account',
  //     name: 'account',
  //     component: Account,
  //     meta: {
  //       featureId: 'account',
  //       featureName: '我的账户'
  //     }
  //   }
  //   ]
  // },
  {
    path: '/login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/auth',
    component: Auth,
    meta: { requiresAuth: false }
  },
  // {
  //   path: '/user',
  //   redirect: '/user/index',
  //   component: Layout,
  //   children: [{
  //     path: '/user/index',
  //     name: 'user',
  //     component: UserList,
  //     meta: {
  //       featureId: 'user',
  //       featureName: '用户管理'
  //     }
  //   }]
  // },
  // {
  //   path: '/group',
  //   redirect: '/group/index',
  //   component: Layout,
  //   children: [{
  //     path: '/group/index',
  //     name: 'group',
  //     component: groupList,
  //     meta: {
  //       featureId: 'group',
  //       featureName: '群组管理'
  //     }
  //   }]
  // },
  {
    path: '/',
    redirect: '/order/index',
    component: Layout,
    children: [{
      path: '/order/index',
      name: 'order',
      component: orderList,
      meta: {
        featureId: 'order',
        featureName: '订单管理'
      }
    },
    {
      path: '/order/details',
      name: 'orderDetails',
      component: orderDetails,
      meta: {
        featureId: 'order',
        featureName: '订单明细'
      }
    },
    {
      path: '/order/create',
      name: 'orderCreate',
      component: orderCreate,
      meta: {
        featureId: 'order',
        featureName: '创建订单'
      }
    }]
  },
  {
    path: '*',
    redirect: 'notFound',
    component: Layout,
    children: [{
      path: '/notFound',
      name: 'notFind',
      component: NotFound,
      meta: { requiresAuth: false, featureId: 'notFound', featureName: '页面未找到' }
    }]
  }
]

Vue.use(Router)

var theRouter = new Router({
  //mode: 'history',  //使用history防止url中出现#
  routes: routes
});

//后置路由事件
theRouter.afterEach((to, from) => {
  console.log('to: ', to.name, ', from: ', from.name);

  //激活当前页面对应的菜单项
  if (from && to.name == from.name) {
    return;
  }

  var featureId = to.meta.featureId;
  if (featureId) {
    // console.log('Current featureId: ', featureId)
    var element = $('.page-sidebar-menu #' + featureId);
    $('.page-sidebar-menu .nav-item.active').removeClass('active');

    if (element.length > 0) {
      element.addClass('active');
      //element.parents('.nav-item').addClass('active');

      element.parents('.nav-item').each(function () {
        $(this).addClass('open').addClass('active');
        $('.arrow', this).addClass('open');
      })
    }
  }
})

// 全局路由监护
theRouter.beforeEach((to, from, next) => {
  var meta = to.meta;
  var requiresAuth = true;
  if (meta.requiresAuth == false)
    requiresAuth = false;

  if (requiresAuth) {
    if (localStorage.getItem('token') != null) {
      next();
    } else {
      // console.log('to: ', to)
      if (from) {
        var returnUrl = to.fullPath;
        next({
          path: '/login',
          query: { redirect: returnUrl }
        });
      } else {
        next({
          path: '/login'
        });
      }
    }
  } else {
    next();
  }
})

export default theRouter;
